package de.busschedule.view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import de.busschedule.R;
import de.busschedule.control.DBHandler;
import de.busschedule.logging.Logger;
 
public class CityBusFragment extends Fragment{
	private ListView list;
	private final String TAG = "CityBusFragment";
	private List<String> lineNames;
	private String[][] lineIds;
	private Map<String, String> lines;
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState){
        final View rootView = inflater.inflate(R.layout.fragment_citybus, container, false);

		//Open the database
		DBHandler handler = new DBHandler(rootView.getContext());
		try{
			handler.createDataBase();
		}catch(IOException e){
			Logger.error(TAG, "Could not create database!");
		}
		try{
			handler.openDataBase();
		}catch(SQLException e){
			Logger.error(TAG, "Could not open database!\n" + e.getMessage());
		}

		//Get the list from the database and then write it to the ListView
        lines = handler.readBusses("city");
        lineNames = createBusList();
        
        list = (ListView) rootView.findViewById(R.id.list_city);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(rootView.getContext(),
				android.R.layout.simple_list_item_1, lineNames);
		list.setAdapter(adapter);

		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
				Intent intent = new Intent(rootView.getContext(), LineDetailActivity.class);
				String id = "";
				for(int i = 0; i < lineIds.length; i++){
					if(lineIds[i][1].equals(list.getAdapter().getItem(arg2))){
						id = lineIds[i][0];
					}
				}
				intent.putExtra("lineID", id);
				startActivity(intent);
			}
		});
        
		handler.close();
        return rootView;
    }

	private List<String> createBusList(){
		List<String> temp = new ArrayList<String>();
		lineIds = new String[lines.size()][2];
		int i = 0;
		for(Map.Entry<String, String> entry : lines.entrySet()) {
			lineIds[i][0] = entry.getKey();
			lineIds[i][1] = entry.getValue();
		    temp.add(entry.getValue());
			i++;
		}
		//Sort the list
		List<String> sortedList = temp.subList(0, temp.size());
		Collections.sort(sortedList);
		return sortedList;
	}
	
	protected void printList(List<?> list){
		System.out.println("====== PrintList =======");
		for(int i = 0; i < list.size(); i++){
			System.out.println(i + ": " + list.get(i).toString());
		}
	}
}